//
//  RootListViewController.m
//  ZhangShangYiBao
//  所有列表试图或者其他视图控制器类的根类
//  Created by 马文涛 on 13-12-12.
//  Copyright (c) 2013年 Mondeo. All rights reserved.
//

#import "RootViewController.h"
#import "MyUITapGestureRecognizer.h"
#import "NewspaperViewController.h"

@interface RootViewController ()
{
    UIView *memuV;
    UILabel *beforeLabel;       // 记录上次被点击的菜单
    
    UIScrollView *imageSV;      // 动态切换图片
    UIPageControl *pageControl;
    int timeCount;
}
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)];
    self.titleLabel.backgroundColor = [UIColor clearColor];  //设置Label背景透明
    self.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:20];  //设置文本字体与大小
    self.titleLabel.textColor = [UIColor colorWithWhite:1 alpha:1];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.text = @"西安晚报";
    self.navigationItem.titleView = self.titleLabel;
}

- (UIViewController*)rootView
{
    UINavigationController *rootNavigationCtr = [[UINavigationController alloc] initWithRootViewController:self];
    rootNavigationCtr.navigationBar.barTintColor = UIColorFromRGB(230, 46, 37, 1);
    self.navigationItem.title = @"西安晚报";
    self.edgesForExtendedLayout = UIRectEdgeNone;       //iOS7属性，试图不会延伸.
    
    // 右边个人中心按钮
    UIImage *centerImg = [UIImage imageNamed:@"profile"];
    centerImg = [centerImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *bbiRight = [[UIBarButtonItem alloc] initWithImage:centerImg style:UIBarButtonItemStylePlain target:self action:@selector(userCenterFun)];
    self.navigationItem.rightBarButtonItem = bbiRight;
    
    memuV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, deviceWidth, 30)];
    [self.view addSubview:memuV];
    
    // 菜单布局
    float x = 10;
    float y = 0;
    float width = (deviceWidth - 20) / 6;
    float height= memuV.frame.size.height;
    NSArray *titleAry = @[@"头条", @"读报", @"科技", @"体育", @"娱乐", @"财经"];
    for (int i = 0; i < titleAry.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x + i * width, y, width, height)];
        label.textAlignment = NSTextAlignmentCenter;
        label.userInteractionEnabled = YES;
        label.tag = i + 1;  // tag值最好从1开始
        // 默认显示头条为红色
        if (i > 0) {
            label.textColor = [UIColor colorWithWhite:0 alpha:0.8];
        }else{
            label.textColor = [UIColor redColor];
            beforeLabel = label;
        }
        label.text = titleAry[i];
        [memuV addSubview:label];
        MyUITapGestureRecognizer *tapGesture = [[MyUITapGestureRecognizer alloc] initWithTarget:self action:@selector(memuFun:)];
        tapGesture.tag = label.tag;
        [label addGestureRecognizer:tapGesture];
    }
    
    // 动态图片
    imageSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, memuV.frame.origin.y + memuV.frame.size.height, deviceWidth, 150)];
    imageSV.delegate = self;
    imageSV.pagingEnabled = YES;
    imageSV.showsHorizontalScrollIndicator = NO;
    imageSV.showsVerticalScrollIndicator = NO;
    //[self.view addSubview:imageSV];
    // 三张图片切换
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"fengJing%d", i]]];
        imageView.tag = i + 1;
        imageView.frame = CGRectMake(i * imageSV.frame.size.width, 0, imageSV.frame.size.width, imageSV.frame.size.height);

        [imageSV addSubview:imageView];
    }
    // scrollRectToVisible此函数要起作用，这里的contentSize属性非常重要，y值不能为0.
    imageSV.contentSize = CGSizeMake(imageSV.frame.size.width * 3, imageSV.frame.size.height);
    
    //使用NSTimer实现定时触发滚动控件滚动的动作。
    timeCount = 0;
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];
    
    // 添加UITableView
    UITableView *newsTV = [[UITableView alloc] initWithFrame:CGRectMake(0, memuV.frame.origin.y + memuV.frame.size.height, deviceWidth, deviceHeight - 93) style:UITableViewStylePlain];
    newsTV.delegate = self;
    newsTV.dataSource = self;
    newsTV.backgroundColor = [UIColor clearColor];
    newsTV.tableHeaderView = imageSV;
    newsTV.tableFooterView = [UIView new];   // 去除多余的显示部分
    [self.view addSubview:newsTV];
    
    // 添加分页控件
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, imageSV.frame.size.height + 15, imageSV.frame.size.width, 10)];
    pageControl.numberOfPages = 3;
    pageControl.currentPage = 0;
    pageControl.enabled = NO;
    [self.view addSubview:pageControl];
    
    return rootNavigationCtr;
}

- (void)scrollTimer
{
    timeCount++;
    if (timeCount == 3) {
        timeCount = 0;
    }
    [imageSV scrollRectToVisible:CGRectMake(timeCount * imageSV.frame.size.width, 0, deviceWidth, imageSV.frame.size.height) animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //    scrollView.contentOffset.x 代表滑动视图横向偏移的距离，左移为正，右移为负
    // 很牛逼的算法
    int page = floor((imageSV.contentOffset.x - deviceWidth / 2) / deviceWidth) + 1;
    pageControl.currentPage = page;
}

- (void)memuFun:(MyUITapGestureRecognizer*)sender
{
    NSInteger tag = sender.tag;
    UILabel *label = (UILabel*)[memuV viewWithTag:tag];
    label.textColor = [UIColor redColor];
    if (label != beforeLabel) {
        beforeLabel.textColor = [UIColor colorWithWhite:0 alpha:0.8];
    }
    
    switch (tag) {
        case 1:
            {
                
            }
            break;
        case 2:
            {
                NewspaperViewController *newspaperVC = [NewspaperViewController new];
                [self.navigationController pushViewController:newspaperVC animated:YES];
            }
            break;
        case 3:
            {
            
            }
            break;
        case 4:
            {
            
            }
            break;
        case 5:
            {
            
            }
            break;
            
        default:
            break;
    }
    
    // 记录上次被点击的菜单
    beforeLabel = label;
}

#pragma mark - UITableViewDataSource代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    int index = row % 3;
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }else{
        while (cell.contentView.subviews.lastObject != nil) {
            [cell.contentView.subviews.lastObject removeFromSuperview];
        }
    }
    
    // 图标
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 50)];
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"pingPang%d", index]];
    [cell.contentView addSubview:imageView];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width + 5, 8, 260, 20)];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.text = @"10岁国球手改日籍战奥运";
    titleLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    titleLabel.textColor = [UIColor colorWithWhite:0 alpha:0.8];
    [cell.contentView addSubview:titleLabel];
    
    // 新闻详情
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x + 2, titleLabel.frame.origin.y + 20, 210, 40)];
    detailLabel.numberOfLines = 0;
    detailLabel.textAlignment = NSTextAlignmentLeft;
    detailLabel.lineBreakMode = NSLineBreakByWordWrapping;
    detailLabel.text = @"2013年8月张智和父亲已经为他填写了日本国籍归化申请表。";
    detailLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
    detailLabel.textColor = [UIColor colorWithWhite:0 alpha:0.5];
    [cell.contentView addSubview:detailLabel];
    
    return cell;
}

#pragma mark - UITableview delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)userCenterFun
{
    
}

- (void)backFun
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    // 显示状态栏
    [UIApplication sharedApplication].statusBarHidden = NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
